package com.jiazhong.mybatis.m3;

import com.jiazhong.mybatis.m3.bean.Emp;
import com.jiazhong.mybatis.m3.mapper.EmpMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

public class App {

    // 探讨了一级缓存
    private static void a() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 查询7908的员工
        Emp emp1 = empMapper.findById(7908);
        System.out.println(emp1);
        // 又查询了7908的员工，这次不需要连接数据库了 使用了缓存（一级缓存）
        Emp emp2 = empMapper.findById(7908);
        System.out.println(emp2);
        Emp emp3 = empMapper.findById(7909);
        System.out.println(emp3);
        sqlSession.close();
    }

    // 探讨一级缓存什么时候失效:必须是一个会话
    private static void b1() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 创建了一个会话
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        EmpMapper empMapper1 = sqlSession1.getMapper(EmpMapper.class);
        Emp emp1 = empMapper1.findById(7908);
        System.out.println(emp1);
        sqlSession1.close();
        // 有创建了一个会话
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        EmpMapper empMapper2 = sqlSession2.getMapper(EmpMapper.class);
        Emp emp2 = empMapper2.findById(7908);
        System.out.println(emp2);
        sqlSession2.close();
    }
    // 探讨一级缓存什么时候失效:手动调用了清空缓存的方法
    private static void b2() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 查询7908的员工
        Emp emp1 = empMapper.findById(7908);
        System.out.println(emp1);
        // 清空缓存
        sqlSession.clearCache();
        // 又查询了7908的员工，这次不需要连接数据库了 使用了缓存（一级缓存）
        Emp emp2 = empMapper.findById(7908);
        System.out.println(emp2);
        sqlSession.close();
    }

    // 探讨一级缓存什么时候失效:当执行更新操作（insert,update和delete）缓存失效
    private static void b3() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession sqlSession = sqlSessionFactory.openSession();
        EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
        // 查询7908的员工
        Emp emp1 = empMapper.findById(7908);
        System.out.println(emp1);
        // 即使失败了也会清空缓存
        int row = empMapper.removeById(9999999);
        System.out.println(row);
        // 又查询了7908的员工，这次不需要连接数据库了 使用了缓存（一级缓存）
        Emp emp2 = empMapper.findById(7908);
        System.out.println(emp2);
        sqlSession.close();
    }

    // 二级缓存
    private static void c() {
        InputStream inputStream = App.class.getClassLoader().getResourceAsStream("config.xml");
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        // 创建了一个会话
        SqlSession sqlSession1 = sqlSessionFactory.openSession();
        EmpMapper empMapper1 = sqlSession1.getMapper(EmpMapper.class);
        Emp emp1 = empMapper1.findById(7908);
        System.out.println(emp1);
        sqlSession1.close();
        // 有创建了一个会话
        SqlSession sqlSession2 = sqlSessionFactory.openSession();
        EmpMapper empMapper2 = sqlSession2.getMapper(EmpMapper.class);
        Emp emp2 = empMapper2.findById(7908);
        System.out.println(emp2);
        sqlSession2.close();
    }
    public static void main(String[] args) {
        c();
    }
}